<template>
	<view class="normal-login-container w-full">
		<view class="logo-content  pt-[15%] w-full items-center justify-center flex">
			<image class="w-[150rpx]" :src="globalConfig.appInfo.logo" mode="widthFix">
			</image>
			<text class="ml-10 text-xl">{{$lang('QMS移动端')}}</text>
		</view>
		<view class="login-form-content">
			<view class="input-item flex items-center">
				<view class="iconfont icon-user icon"></view>
				<uni-easyinput v-model="loginForm.username" class="input" type="text" :placeholder="$lang('请输入账号')"
					maxlength="30" />
			</view>
			<view class="input-item flex items-center">
				<view class="iconfont icon-password icon"></view>
				<uni-easyinput v-model="loginForm.password" type="password" class="input" :placeholder="$lang('请输入密码')"
					maxlength="20" />
			</view>
			<view class="input-item flex items-center" style="width: 60%;margin: 0px;" v-if="captchaEnabled">
				<view class="iconfont icon-code icon"></view>
				<uni-easyinput v-model="loginForm.code" type="number" class="input" :placeholder="$lang('请输入验证码')"
					maxlength="4" />
				<view class="h-[38px] float-right">
					<image :src="codeUrl" @click="getCode" class="w-[200rpx] ml-2.5 absolute h-[38px]"></image>
				</view>
			</view>
			<button @click="handleLogin" type="primary"
				class="m-10 h-[40px] leading-[40px] text-[#ffffff] mt-[15px] rounded-[50px] text-20">{{$lang('登录')}}</button>
			<view style="display: flex; justify-content: space-between; align-items: center">
				<view class="text-center mt-10">
					<text class="text-[#b5b3b3]">{{$lang('版本')}}：</text>
					<text @click="handleUserAgrement" class="text-[#070606]">V{{globalConfig.appInfo.version}}</text>
				</view>
				<span class="mt-10">
					<text class="text-[#056ad7]" @click="handleTab('/pages/register')">{{ $lang('注册账号') }}</text>
					<text class="forget-pwd text-[#b5b3b3]"
						@click="handleTab('/pages/resetPassWord')">{{ $lang('忘记密码') }}</text>
				</span>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		getCodeImg
	} from '@/api/login'
	import useUserStore from '@/store/modules/user'
	import usePermissionStore from '@/store/modules/permission'

	export default {
		data() {
			return {
				codeUrl: "",
				captchaEnabled: true,
				// 用户注册开关
				register: true,
				globalConfig: getApp().globalData.config,
				loginForm: {
					username: "",
					password: "",
					code: "",
					uuid: ''
				}
			}
		},
		created() {
			this.getCode()
			console.log(this.$i18n.locale)
		},
		methods: {
			// 用户注册
			handleTab(path) {
				this.$tab.redirectTo(path)
			},
			// 用户协议
			handleUserAgrement() {
				let site = this.globalConfig.appInfo.agreements[0]
				this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
			},
			// 获取图形验证码
			getCode() {
				getCodeImg().then(res => {
					this.captchaEnabled = res.data.captchaOff === "off" ? false : true
					if (this.captchaEnabled) {
						this.codeUrl = 'data:image/gif;base64,' + res.data.img
						this.loginForm.uuid = res.uuid
					}
				})
			},
			// 登录方法
			async handleLogin() {
				if (this.loginForm.username === "") {
					this.$modal.msgError(this.$lang("请输入您的账号"))
				} else if (this.loginForm.password === "") {
					this.$modal.msgError(this.$lang("请输入您的密码"))
				} else if (this.loginForm.code === "" && this.captchaEnabled) {
					this.$modal.msgError(this.$lang("请输入验证码"))
				} else {
					this.$modal.loading(this.$lang("登录中，请耐心等待..."))
					this.pwdLogin()
				}
			},
			// 密码登录
			async pwdLogin() {
				useUserStore().Login(this.loginForm).then(() => {
					this.loginSuccess()
				}).catch(() => {
					if (this.captchaEnabled) {
						this.getCode()
					}
				}).finally(() => {
					this.$modal.closeLoading()
				})
			},
			// 登录成功后，处理函数
			loginSuccess(result) {
				// 设置用户信息
				useUserStore().GetInfo().then(res => {
					usePermissionStore().generateRoutes()
					this.$tab.reLaunch('/pages/index')
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.forget-pwd {
		margin-left: 10px;
		cursor: pointer;
		border-left: 1px solid;
		padding-left: 10px;
	}

	.normal-login-container {
		.login-form-content {
			@apply my-5 mx-5 mt-[15%] text-center;

			.input-item {
				 @apply my-10 h-[45px] rounded-[12px] mr-15 !important;
				.icon {
					font-size: 38rpx;
					margin-left: 10px;
					color: #9d9d9d;
				}

				.input {
					width: 100%;
					font-size: 14px;
					line-height: 20px;
					text-align: left;
					padding-left: 15px;
				}

			}


		}
	}
</style>